package com.opensource.svgaplayer.entities;

import com.opensource.svgaplayer.utils.SVGAPoint;
import ohos.agp.render.Path;
import ohos.agp.utils.Point;
import ohos.agp.utils.TextTool;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

/**
 * ex.
 *
 * @author wl
 * @since 2021-06-28
 */
public class SVGAPathEntity {
    private Set<String> VALID_METHODS = new HashSet<>();
    private String replacedValue;
    private Path cachedPath;

    /**
     * SVGAPathEntity
     *
     * @param originValue String
     */
    public SVGAPathEntity(String originValue) {
        VALID_METHODS.add("M");
        VALID_METHODS.add("L");
        VALID_METHODS.add("H");
        VALID_METHODS.add("V");
        VALID_METHODS.add("C");
        VALID_METHODS.add("S");
        VALID_METHODS.add("Q");
        VALID_METHODS.add("R");
        VALID_METHODS.add("A");
        VALID_METHODS.add("m");
        VALID_METHODS.add("l");
        VALID_METHODS.add("h");
        VALID_METHODS.add("v");
        VALID_METHODS.add("c");
        VALID_METHODS.add("s");
        VALID_METHODS.add("q");
        VALID_METHODS.add("r");
        VALID_METHODS.add("a");
        VALID_METHODS.add("z");
        if (originValue.contains(",")) {
            replacedValue = originValue.replace(",", " ");
        } else {
            replacedValue = originValue;
        }
    }

    /**
     * buildPath
     *
     * @param toPath Path
     */
    public void buildPath(Path toPath) {
        if (this.cachedPath != null) {
            toPath.set(this.cachedPath);
            return;
        }
        Path cachedPath = new Path();
        StringTokenizer segments = new StringTokenizer(this.replacedValue, "MLHVCSQRAZmlhvcsqraz", true);
        String currentMethod = "";
        while (segments.hasMoreTokens()) {
            String segment = segments.nextToken();
            if (segment.isEmpty()) {
                continue;
            }
            if (VALID_METHODS.contains(segment)) {
                currentMethod = segment;
                if (currentMethod.equals("Z") || currentMethod.equals("z")) {
                    operate(cachedPath, currentMethod, new StringTokenizer("", ""));
                }
            } else {
                operate(cachedPath, currentMethod, new StringTokenizer(segment, " "));
            }
        }
        this.cachedPath = cachedPath;
        toPath.set(cachedPath);
    }

    /**
     * operate
     *
     * @param finalPath Path
     * @param method    String
     * @param args      StringTokenizer
     */
    private void operate(Path finalPath, String method, StringTokenizer args) {
        float x0 = 0.0F;
        float y0 = 0.0F;
        float x1 = 0.0F;
        float y1 = 0.0F;
        float x2 = 0.0F;
        float y2 = 0.0F;
        try {
            int index = 0;
            while (args.hasMoreTokens()) {
                String s = args.nextToken();
                if (TextTool.isNullOrEmpty(s)) {
                    continue;
                }
                if (index == 0) {
                    x0 = Float.parseFloat(s);
                }
                if (index == 1) {
                    y0 = Float.parseFloat(s);
                }
                if (index == 2) {
                    x1 = Float.parseFloat(s);
                }
                if (index == 3) {
                    y1 = Float.parseFloat(s);
                }
                if (index == 4) {
                    x2 = Float.parseFloat(s);
                }
                if (index == 5) {
                    y2 = Float.parseFloat(s);
                }
                index++;
            }
        } catch (Exception var14) {
        }
        SVGAPoint currentPoint = new SVGAPoint(0.0f, 0.0f, 0.0f);
        if (method.equals("M")) {
            finalPath.moveTo(x0, y0);
            currentPoint = new SVGAPoint(x0, y0, 0.0F);
        } else if (method.equals("m")) {
            finalPath.rMoveTo(x0, y0);
            currentPoint = new SVGAPoint(new BigDecimal(currentPoint.getX()).add(new BigDecimal(x0)).floatValue(), new BigDecimal(currentPoint.getY()).add(new BigDecimal(y0)).floatValue(), 0.0F);
        }
        if (method.equals("L")) {
            finalPath.lineTo(x0, y0);
        } else if (method.equals("l")) {
            finalPath.rLineTo(x0, y0);
        }
        if (method.equals("C")) {
            finalPath.cubicTo(x0, y0, x1, y1, x2, y2);
        } else if (method.equals("c")) {
            finalPath.cubicTo(new Point(x0, y0), new Point(x1, y1), new Point(x2, y2));
        }
        if (method.equals("Q")) {
            finalPath.quadTo(x0, y0, x1, y1);
        } else if (method.equals("q")) {
            finalPath.rQuadTo(x0, y0, x1, y1);
        }
        if (method.equals("H")) {
            finalPath.lineTo(x0, currentPoint.getY());
        } else if (method.equals("h")) {
            finalPath.rLineTo(x0, 0.0f);
        }
        if (method.equals("V")) {
            finalPath.lineTo(currentPoint.getX(), x0);
        } else if (method.equals("v")) {
            finalPath.rLineTo(0.0F, x0);
        }
        if (method.equals("Z")) {
            finalPath.close();
        } else if (method.equals("z")) {
            finalPath.close();
        }
    }
}
